<template>
	<view class="pages">
		<view class="top">
			<view class="nav">
				{{info.subjectName || '**'}} > {{info.subjectName2 || '**'}} > {{info.treeName || '**'}} >
				{{info.chapterName || '**'}} > {{info.name || '**'}}
			</view>
			<view class="search">
				<u-search bg-color="#FFFFFF" placeholder="请输入知识标题" v-model="keyword" @search="serach"
					@custom="serach"></u-search>
			</view>
		</view>
		<!-- 占位 -->
		<view style="height: 184rpx;"></view>
		<view class="list u-p-l-20 u-p-r-20 bg-white" v-if="list && list.length">
			<view class="item flex u-p-t-20 u-p-b-20" @click="openKnowLedgeDetail(item)" v-for="(item,index) in list"
				:key="item.id">
				<view class="flex-shrink u-font-24 tag text-white" :style="[handleTypeBg(item.type)]">
					{{item.type | handleType}}
				</view>
				<view class="u-m-l-20">
					<view class="word-break-all u-font-28 color333">{{item.title || ''}}</view>
					<view v-if="item.createTime" class="u-font-24 color999 u-m-t-30">{{item.createTime}}</view>
				</view>
			</view>
			<view class="u-p-t-20 u-p-b-20">
				<u-loadmore :status="status" font-size="24" />
			</view>
		</view>
		<!-- 空状态 -->
		<view class="u-p-t-50" v-if="!list.length">
			<uni-nodata></uni-nodata>
		</view>
	</view>
</template>

<script>
	import {
		KnowledgeDel,
		KnowledgeAdd,
		KnowledgeQuery,
		KnowledgeUpdate,
		KnowledgeQueryId,
	} from '@/api/send.js'
	export default {
		data() {
			return {
				status: "loadmore",
				info: null,
				keyword: "",
				page: 1,
				list: []
			}
		},
		onLoad(e) {
			if (e.info) {
				this.info = JSON.parse(e.info);
				this.getList();
			}
		},
		filters: {
			handleType(val) {
				let str = '';
				switch (val) {
					case 1:
						str = "答"
						break;
					case 2:
						str = "记"
						break;
					case 3:
						str = "资"
						break;
					case 4:
						str = "辅"
						break;
					case 5:
						str = "直"
						break;
					case 6:
						str = "文"
						break;
				}
				return str;
			}
		},
		onReachBottom() {
			if (this.status == 'nomore') return;
			this.page++;
			this.status = 'loading';
			this.getList();
		},
		methods: {
			//搜索
			serach() {
				this.page = 1;
				this.getList();
			},
			getList() {
				uni.showLoading({
					mask: true
				});
				if (this.page == 1) this.list = [];
				KnowledgeQuery({
					knowledgeName: this.keyword,
					page: this.page,
					size: 20,
					parentId: this.info.id
				}).then(res => {
					if (res.code == 100000) {
						let list = [];
						list = res.data.list.length && res.data.list.map(e => {
							return {
								id: e.id,
								title: e.knowledgeName,
								type: e.type,
								createTime: e.createTime
							}
						})
						this.list.push(...list);
						if (!res.data.list || res.data.list.length < 20) {
							this.status = 'nomore';
						} else {
							this.status = 'loading';
						}
					}
				}).finally(() => {
					uni.hideLoading();
				})
			},
			//打开知识
			openKnowLedgeDetail(row) {
				//打开不同的详情
				uni.showToast({
					title: '打开文章详情',
					icon: 'none'
				});
			},
			handleTypeBg(val) {
				let str = '';
				let style = {};
				switch (val) {
					case 1:
						str = "rgba(25, 204, 165, .23)"
						break;
					case 2:
						str = "rgba(94, 116, 139, 1)"
						break;
					case 3:
						str = "rgba(25, 204, 165, .8)"
						break;
					case 4:
						str = "rgba(255, 113, 1, .85)"
						break;
					case 5:
						str = "rgba(170, 255, 127, 0.8)"
						break;
					case 6:
						str = "rgba(255, 85, 255, 0.8)"
						break;
				}
				style.backgroundColor = str;
				return style;
			}
		}
	}
</script>

<style lang="scss" scoped>
	.pages {
		min-height: 100vh;
		background-color: #FFF;

		.top {
			position: fixed;
			top: 0;
			left: 0;
			right: 0;
		}

		.nav {
			color: #73839A;
			border: 2rpx solid #EBEEEF;
			height: 100rpx;
			font-size: 28rpx;
			display: flex;
			align-items: center;
			word-break: break-all;
			padding-left: 30rpx;
			padding-right: 30rpx;
			background-color: #FFFFFF;
		}

		.search {
			padding: 10rpx 64rpx;
			background-color: #F6F8F6;
		}

		.list {
			.item {
				border-bottom: 2rpx dashed #EBEEEF;
			}

			.tag {
				border-radius: 8rpx;
				width: 40rpx;
				height: 40rpx;
				display: flex;
				align-items: center;
				justify-content: center;
			}
		}
	}
</style>